package com.yw.atp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static String DateFormat(Date d,String f){
    	if(d!=null){
    		Calendar cal = Calendar.getInstance();
    		cal.setTime(d);
    		int year = cal.get(Calendar.YEAR);
    		int month = cal.get(Calendar.MONTH)+1;
    		int date = cal.get(Calendar.DAY_OF_MONTH);
    		int hour = cal.get(Calendar.HOUR_OF_DAY);
    		int minute = cal.get(Calendar.MINUTE);
    		if("YYYY/MM/DD HH:mmAP".equals(f)){
    			String str =year+"/"+(month+1)+"/"+date+" ";
    			if((hour+minute)>0||(hour==23&&minute==59)){
    				if(hour>=12){
    					str += (hour-12)+":"+minute+"p";	
    				}else{
    					str += hour+":"+minute;
    				}	
    			}
    			return str;
    		}else{    		
    			String Y,M,D,H,m;
    			Y = ""+year;
    			M = month<10?"0"+month:""+month;
    			D = date<10?"0"+date:""+date;
    			if((hour==0&&minute==0)||(hour==23&&minute==59)){
    				H = "";
    				m = "";
    			}else{
    				H = hour<10?"0"+hour:""+hour;
    				m = minute<10?"0"+minute:""+minute;
    			}
    			return Y+M+D+H+m;
    		}
    	}else{
    		return "";
    	}
    }
    
    public static Date parseDate(String d){
    	Calendar cal = Calendar.getInstance();
    	int Y,M,D,H,m;
    	if(d.indexOf("/")!=-1){
    		String ymd = d.split(" ")[0];
    		Y = Integer.valueOf(ymd.split("/")[0]);
    		M = (Integer.valueOf(ymd.split("/")[1])-1);
    		D = Integer.valueOf(ymd.split("/")[2]);
    		if(d.split(" ").length==2&&d.split(" ")[1].indexOf(":")!=-1){
    			String hm = d.split(" ")[1];
    			int base = 0;
    			if(hm.indexOf("p")!=-1){
    				hm = hm.split("p")[0];
    				base = 12;
    			}
    			H = Integer.valueOf(hm.split(":")[0])+base;
    			m = Integer.valueOf(hm.split(":")[1]);
    		}else{
    			H = 0;
    			m = 0;
    		}
    	}else{
    		Y = Integer.valueOf(d.substring(0,4));
        	M = Integer.valueOf(d.substring(4,6)) - 1;
        	D = Integer.valueOf(d.substring(6,8));
        	if(d.length()>8){
        		H = Integer.valueOf(d.substring(8,10));
        		m = Integer.valueOf(d.substring(10,12));
        	}else{
        		H = 0;
        		m = 0;
        	}
    	}
    	cal.set(Calendar.YEAR,Y);
		cal.set(Calendar.MONTH,M);
		cal.set(Calendar.DAY_OF_MONTH,D);
		cal.set(Calendar.HOUR_OF_DAY,H);
		cal.set(Calendar.MINUTE,m);
		cal.set(Calendar.SECOND,0);
		return cal.getTime();
    }  
    
    public static String getServiceFormat(Date date){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(date);
    }
    
    public static String getServiceFormatTime(Date date){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	return sdf.format(date);
    }
    
    public static String getClientFormatA(Date date){
    	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm");
    	return sdf.format(date);
    }
    
    public static String getClientFormatB(Date date){
    	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
    	return sdf.format(date);
    }
    
    public static Date parClientData(String d){
    	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm");
    	try {
			return getRealDateB(sdf.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
    }
    
	  
   public static Date getRealDateB(Date date){
	  Calendar cal = Calendar.getInstance();
	  Calendar c = Calendar.getInstance();
	  c.setTime(date);
	  c.set(Calendar.YEAR,cal.get(Calendar.YEAR));
	  return c.getTime();
   }
   
   public static void main(String[] args) {
	   System.out.println(parClientData("10/1 22:22"));
   }
}
